package cn.wolfcode.cloud.productserver.controller;

import cn.wolfcode.cloud.productapi.domian.Product;
import cn.wolfcode.cloud.productapi.service.IProductService;
import cn.wolfcode.cloud.productapi.service.ProductFeignApi;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class ProductFeignClient implements ProductFeignApi {

    @Autowired
    private IProductService productService;

    @Value("${server.port}")
    private String port;

    @Override
    @HystrixCommand(fallbackMethod = "getProductFall")
    public Product getProduct(Long id) {
        log.info("商品服务 接受的参数:id={}",id);

        Product product = productService.get(id);
        Product result = new Product();
        BeanUtils.copyProperties(product,result);
        result.setName(result.getName()+",data from "+port);

        /*try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        log.info("商品服务 响应的参数:{}",result);
        return result;
    }

    public Product getProductFall(Long id) {

        log.info("商品服务降级...");

        return new Product();
    }
}